package com.sunny.ynhz.ui.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunny.ynhz.utils.MyPDialog;

import butterknife.ButterKnife;

/**
 * Created by yangxianzhu on 2016/5/4 0004.
 */
public abstract class BaseFragmentWithViewPager extends Fragment {

    /**
     * 当前是否可见
     */
    protected boolean isVisible = false;
    public LayoutInflater inflater;
    public View mContentView;
    /**
     * 是否已被加载过一次，第二次就不再去请求数据了
     */
    protected boolean hasLoaded = false;

    /**
     * 标志位，标志已经初始化完成
     */
    protected boolean isPrepared = false;
    protected MyPDialog myPDialog;
    private Bundle arguments;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createview();
    }

    /**
     * 当前fragment不可见时调用
     */
    protected void onInvisible() {

    }

    protected void createview() {

    }

    /**
     * 当前fragment 由可见 ---> 不可见时调用
     */
    protected void onVisible() {
        initData();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflater = LayoutInflater.from(getActivity());
        hasLoaded=false;
        mContentView = inflater.inflate(setLayoutResourceID(), null);//setContentView(inflater, container);
        ButterKnife.bind(this, mContentView);
        arguments = getArguments();
        setUpView();
        return mContentView;
    }

    protected abstract int setLayoutResourceID();

    protected abstract void initData();

    public void showDialog(String message) {
        if (myPDialog == null) {
            myPDialog = new MyPDialog(getActivity());
        }
        myPDialog.showDialog(message);
    }

    protected void setUpView() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mContentView=null;
        hasLoaded=false;//没有加载了;

    }

    public void startActivity(Intent intent, Class<?> cls) {
        getActivity().startActivity(intent);
    }

}
